package com.qf.shopping.servlet.backController;

import com.qf.shopping.common.Constants;
import com.qf.shopping.common.DisatpcherServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**管理员登录
 * @author 三旬
 * @version 1.0  2022/9/20
 */
@WebServlet("/AdminController/*")
public class AdminController extends DisatpcherServlet {

    public String login(HttpServletRequest request, HttpServletResponse response) {

        // 1.获取表单数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username);
        // 2.调用业务，这里不查询数据库了，真正的登录流程在前端来实现
        if ("admin".equals(username) && "1".equals(password)) {
            // 登录成功,把登录的用户信息保存到session中
            request.getSession().setAttribute("sysUser", username);
//            request.setAttribute("sysUser", username);
            // 跳转后台首页
            return Constants.REDIRECT + ":" + "back/main.jsp";
        } else if ("toString".equals(username) && "123".equals(password)) {
            request.getSession().setAttribute("sysUser", username);
            return Constants.REDIRECT + ":" + "back/main.jsp";
        } else {
            // 登录失败
            return Constants.SCRIPT + ":" + "登录失败";
        }
    }

    public String logout(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().invalidate();
        return Constants.FORWARD + ":" + "backLogin.jsp";
    }
}
